package com.fly.controller;

import com.fly.bean.*;
import com.fly.service.FixTaskService;
import com.fly.service.TaskServiceL;

import com.fly.util.JWTUtil;
import com.fly.util.Msg;
import com.fly.util.Page;
import com.fly.vo.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TaskControllerL {
    @Resource
    TaskServiceL taskServiceL;

    @Resource
    FixTaskService fixTaskService;


    @RequestMapping("/getAllTaskByUserId")
    @ResponseBody
    public Msg getAllTaskByUserId(Page page, HttpServletRequest request){
        String token=request.getHeader("Authorization");
        Users users=JWTUtil.unsign(token,Users.class);
        Integer userId=null;
        UserIdPageVO userIdPageVO=new UserIdPageVO();

        if(users!=null){
            userId=users.getUserId();
            userIdPageVO.setRoleId(fixTaskService.selectUserRoleByUserId(users.getUserId()));

        }

        //根据用户ID和分页查询任务
        userIdPageVO.setUserId(userId);
        userIdPageVO.setPage(page);
        List<Task> tasks = taskServiceL.selectAllTaskByUserId(userIdPageVO);
        //根据用户ID查询总数量
        Integer pageCount=taskServiceL.selectCountTaskByUserId(userIdPageVO);

        page.setCount(pageCount);
        Msg msg=Msg.success();

        msg.add("count",page.getCount());
        msg.add("tasks",tasks);

        return msg;
    }

    @RequestMapping("/getAllTaskByConditionL")
    @ResponseBody
    public Msg getAllTaskByCondition(String taskNo, String circuitryNo, Integer taskState, String userName, Date startDate, Date endDate, Page page,HttpServletRequest request){
        String token=request.getHeader("Authorization");
        Users users=JWTUtil.unsign(token,Users.class);
        if(taskNo==""){
            taskNo=null;
        }
        if(circuitryNo==""){
            circuitryNo=null;
        }
        if(userName==""){
            userName=null;
        }
        TaskDatePageVo taskDatePageVo=new TaskDatePageVo();
        Task task=new Task();
        Circuitry circuitry=new Circuitry();
        SystemPropertiesValue systemPropertiesValue=new SystemPropertiesValue();
        taskDatePageVo.setTask(task);
        task.setUsers(users);
        task.setCircuitry(circuitry);
        task.setSystemPropertiesValue(systemPropertiesValue);
        taskDatePageVo.getTask().setTaskNo(taskNo);
        taskDatePageVo.getTask().getUsers().setUserName(userName);
        taskDatePageVo.getTask().getCircuitry().setCircuitryNo(circuitryNo);
        taskDatePageVo.getTask().getSystemPropertiesValue().setId(taskState);
        taskDatePageVo.setEndDate(endDate);
        taskDatePageVo.setStartDate(startDate);
        taskDatePageVo.setPage(page);
        taskDatePageVo.setUserId(users.getUserId());
        taskDatePageVo.setRoleId(fixTaskService.selectUserRoleByUserId(users.getUserId()));
        List<Task> tasks=taskServiceL.selectAllTaskByCondition(taskDatePageVo);

        Integer pageCount=taskServiceL.selectCountTaskByCondition(taskDatePageVo);
        Msg msg =Msg.success();

        page.setCount(pageCount);

        msg.add("count",page.getCount());
        msg.add("tasks",tasks);
        return msg;
    }

    @RequestMapping("/changeTaskSateToRunning")
    @ResponseBody
    public Msg changeTaskState(Integer taskId){
        Msg msg=Msg.success();
        taskServiceL.changeTaskState(taskId);
        return msg;
    }

    @RequestMapping("/showTaskandPoles")
    @ResponseBody
    public Msg showTask(Integer taskId){
        Msg msg=Msg.success();
        TaskPoleVO VO=taskServiceL.selectTaskAndPoles(taskId);
        msg.add("taskAndPoles",VO);
        return msg;
    }

    @RequestMapping("/showPoleMsgByPoleId")
    @ResponseBody
    public Msg showPoleMsgByPoleId(Integer poleId){
        Msg msg=Msg.success();
        PoleMsgVO VO=taskServiceL.selectMsgByPoleId(poleId);
        msg.add("taskAndPoles",VO);
        return msg;
    }

    @RequestMapping("/insertRecord")
    @ResponseBody
    public Msg insertRecord(String taskNo,Integer poleId,Integer defectsId,String defectsLevel,Date findDate,String defectsDescribe,HttpServletRequest request){
        Msg msg=Msg.success();

        String token=request.getHeader("Authorization");
        Users users=JWTUtil.unsign(token,Users.class);
        Integer hasDefects;
        if(defectsId==100000){
            hasDefects=0;
        }else{
            hasDefects=1;
        }


        Task task=taskServiceL.selectTask(taskNo);

        Pole pole=new Pole();
        pole.setPoleId(poleId);

        Defects defects=new Defects();
        defects.setDefectsId(defectsId);

        DamageRecord damageRecord=new DamageRecord();
        damageRecord.setPole(pole);
        damageRecord.setFindDate(findDate);
        damageRecord.setDefectsLevel(defectsLevel);
        damageRecord.setDefects(defects);
        damageRecord.setDefectsDescribe(defectsDescribe);
        damageRecord.setUsers(users);

        DamageRecordTaskVO damageRecordTaskVO=new DamageRecordTaskVO();
        damageRecordTaskVO.setDamageRecord(damageRecord);
        damageRecordTaskVO.setHasDefects(hasDefects);
        damageRecordTaskVO.setTask(task);

        if(taskServiceL.selectDamageRecordByPoleId(poleId)!=null){
            taskServiceL.updateRecordByPoleId(damageRecord);
        }else {
            taskServiceL.insertRecordByPoleId(damageRecord);
        }
        taskServiceL.insertReceiptByTaskId(damageRecordTaskVO);
        return msg;
    }

    @RequestMapping("/updateRecord")
    @ResponseBody
    public Msg updateRecord(Integer poleId,Integer defectsId,String defectsLevel,Date findDate,String defectsDescribe, HttpServletRequest request){
        String token=request.getHeader("Authorization");
        Users users=JWTUtil.unsign(token,Users.class);
        Msg msg=Msg.success();
        DamageRecord damageRecord=new DamageRecord();

//        String token=request.getHeader("Authorization");
//
//        Users users=JWTUtil.unsign(token,Users.class);
//        if(users!=null){
//            damageRecord.setUsers(users);
//        }
        damageRecord.setUsers(users);
        Defects defects=new Defects();
        Pole pole=new Pole();
        pole.setPoleId(poleId);
        defects.setDefectsId(defectsId);


        damageRecord.setDefectsLevel(defectsLevel);
        damageRecord.setDefectsDescribe(defectsDescribe);
        damageRecord.setDefects(defects);
        damageRecord.setFindDate(findDate);
        damageRecord.setPole(pole);

        taskServiceL.updateRecordByPoleId(damageRecord);
        return msg;
    }

    @RequestMapping("/selectRecord")
    @ResponseBody
    public Msg selectRecordByPoleId(Integer poleId){
        Msg msg=Msg.success();

        CircuitryPoleRecordVO circuitryPoleRecordVO=taskServiceL.selectRecordByPoleId(poleId);
        msg.add("circuitryPoleRecordVO",circuitryPoleRecordVO);
        return msg;
    }

    @RequestMapping("/selectAllDefects")
    @ResponseBody
    public Msg selectAllDefects(Page page){
        Msg msg=Msg.success();

        List<DefectsVO> defectsVO=taskServiceL.selectAllDefects(page);

        Integer pageCount=taskServiceL.selectCountDefects();
        page.setCount(pageCount);

        msg.add("count",page.getCount());

        msg.add("defectsVO",defectsVO);

        return msg;
    }

    @RequestMapping("/selectDefectsByCondition")
    @ResponseBody
    public Msg selectDefectsByCondition(findDefectsVO findDefectsVO,Page page){
        Msg msg=Msg.success();
//        findDefectsVO findDefectsVO=new findDefectsVO();
//        findDefectsVO.setTaskNo(taskNo);
//        findDefectsVO.setCircuitryNo(circuitryNo);
//        findDefectsVO.setDefectsName(defectsName);
//        findDefectsVO.setDefectsLevel(defectsLevel);
//        findDefectsVO.setFindStartDate(findStartDate);
//        findDefectsVO.setFindEndDate(findEndDate);
//        findDefectsVO.setPerformStartDate(performStartDate);
//        findDefectsVO.setPerformEndDate(performEndDate);
        //        传过来的参没有值就设置为空,有就在参数前后添加%%模糊
        if (findDefectsVO.getTaskNo() == "") {
            findDefectsVO.setTaskNo(null);
        }
        if (findDefectsVO.getCircuitryNo() == "") {
            findDefectsVO.setCircuitryNo(null);
        }
        if (findDefectsVO.getDefectsId() == "") {
            findDefectsVO.setDefectsId(null);
        }

        if (findDefectsVO.getDefectsLevel() == "") {
            findDefectsVO.setDefectsLevel(null);
        }

        findDefectsVO.setPage(page);
        List<DefectsVO> defectsVO=taskServiceL.selectDefectsByCondition(findDefectsVO);

        Integer pageCount=taskServiceL.selectCountDefectsByCondition(findDefectsVO);
        page.setCount(pageCount);

        msg.add("count",page.getCount());
        msg.add("defectsVO",defectsVO);

        return msg;
    }

    @RequestMapping("/changeDefectsLevel")
    @ResponseBody
    public Msg changeDefectsLevel(DamageIdLevelVO damageIdLevelVO){
        Msg msg=Msg.success();

        taskServiceL.changeDefectsLevel(damageIdLevelVO);


        return msg;
    }


    @RequestMapping("/selectAllDefectsIsNull")
    @ResponseBody
    public Msg selectAllDefectsIsNull(Page page){
        Msg msg=Msg.success();

        List<DefectsVO> defectsVO=taskServiceL.selectAllDefectsIsNull(page);

        Integer pageCount=taskServiceL.selectCountDefectsIsNull(page);
        page.setCount(pageCount);

        msg.add("count",page.getCount());
        msg.add("defectsVO",defectsVO);

        return msg;
    }

    @RequestMapping("/selectAllDefectsIsNullByCondition")
    @ResponseBody
    public Msg selectAllDefectsIsNullByCondition(findDefectsIsNullVO findDefectsIsNullVO,Page page){
        Msg msg=Msg.success();
        findDefectsIsNullVO.setPage(page);
        //        传过来的参没有值就设置为空,有就在参数前后添加%%模糊
        if (findDefectsIsNullVO.getTaskNo() == "") {
            findDefectsIsNullVO.setTaskNo(null);
        }
        if (findDefectsIsNullVO.getCircuitryNo() == "") {
            findDefectsIsNullVO.setCircuitryNo(null);
        }
        if (findDefectsIsNullVO.getPoleNo() == "") {
            findDefectsIsNullVO.setPoleNo(null);
        }

        if (findDefectsIsNullVO.getUserName() == "") {
            findDefectsIsNullVO.setUserName(null);
        }


        List<DefectsVO> defectsVO=taskServiceL.selectAllDefectsIsNullByCondition(findDefectsIsNullVO);

        Integer pageCount=taskServiceL.selectCountDefectsIsNullByCondition(findDefectsIsNullVO);
        page.setCount(pageCount);

        msg.add("count",page.getCount());
        msg.add("defectsVO",defectsVO);

        return msg;
    }

    @RequestMapping("/getDefectsName")
    @ResponseBody
    public Msg getDefectsName(){
        Msg msg=Msg.success();

        List<Defects> defects=taskServiceL.selectDefectsName();
        msg.add("defects",defects);

        return msg;
    }


}
